package com.zwl9517hotmail.libplaymedia;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.WindowManager;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

import cn.jzvd.JZVideoPlayer;
import cn.jzvd.JZVideoPlayerManager;

/**
 * Created by ZouWeiLin
 * on 2017/11/24  17:35
 * version:
 * desc:
 */
public class PlayMediaActivity extends AppCompatActivity {

    private static final String KEY_DATA = "KEY_DATA";
    private static final String KEY_REPEAT = "KEY_REPEAT";
    private static final String KEY_REPEAT_COUNT = "KEY_REPEAT_COUNT";

    private LinkedHashMap<String, String> linkedMap = new LinkedHashMap<>();
    private boolean needRepeat;
    private int repeatCount;

    /**
     * @param context     上下文
     * @param list        地址，可以是本地视频地址，也可以是url
     * @param needRepeat  {@code true}需要重复播放; {@code false} 不需要重复播放
     * @param repeatCount 重复播放次数，目前暂无功能
     */
    public static void start(Context context, List<String> list, boolean needRepeat, int repeatCount) {
        Intent intent = new Intent();
        intent.setClass(context, PlayMediaActivity.class);
        if (!(context instanceof Activity)) {
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }
        Bundle bundle = new Bundle();
        bundle.putStringArrayList(KEY_DATA, (ArrayList<String>) list);
        bundle.putBoolean(KEY_REPEAT, needRepeat);
        bundle.putInt(KEY_REPEAT_COUNT, repeatCount);
        intent.putExtra("key_bundle", bundle);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setFullScreen();
        setContentView(R.layout.activity_play_media);

        initData();
        JZVideoPlayer.FULLSCREEN_ORIENTATION = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
        JZVideoPlayer.NORMAL_ORIENTATION = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;

//        linkedMap.put("0", "http://jzvd.nathen.cn/c494b340ff704015bb6682ffde3cd302/64929c369124497593205a4190d7d128-5287d2089db37e62345123a1be272f8b.mp4");
//        linkedMap.put("1", "http://jzvd.nathen.cn/63f3f73712544394be981d9e4f56b612/69c5767bb9e54156b5b60a1b6edeb3b5-5287d2089db37e62345123a1be272f8b.mp4");
//        linkedMap.put("2", "http://jzvd.nathen.cn/b201be3093814908bf987320361c5a73/2f6d913ea25941ffa78cc53a59025383-5287d2089db37e62345123a1be272f8b.mp4");
//        linkedMap.put("3", "http://jzvd.nathen.cn/d2438fd1c37c4618a704513ad38d68c5/68626a9d53ca421c896ac8010f172b68-5287d2089db37e62345123a1be272f8b.mp4");
//        linkedMap.put("0", Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "com.zwl9517hotmail.demotakevideo/RecordMedia/2017-11-24_19时25分58秒.mp4");
//        linkedMap.put("1", "/storage/emulated/0/com.zwl9517hotmail.demotakevideo/RecordMedia/2017-11-24_19时25分41秒.mp4");
//        linkedMap.put("2", "/storage/emulated/0/com.zwl9517hotmail.demotakevideo/RecordMedia/2017-11-24_19时25分20秒.mp4");
        getSupportFragmentManager().beginTransaction()
                .replace(R.id.p_m_container, PlayMediaFragment.newInstance(linkedMap, needRepeat, repeatCount))
                .commit();
    }

    @Override
    public void onBackPressed() {
        cancelTimer();
        JZVideoPlayer.backPress();
        super.onBackPressed();
    }

    @Override
    protected void onPause() {
        super.onPause();
        cancelTimer();
        JZVideoPlayer.releaseAllVideos();
    }

    private void setFullScreen() {
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
    }

    private void initData() {
        Bundle bundle = getIntent().getBundleExtra("key_bundle");
        ArrayList<String> list = bundle.getStringArrayList(KEY_DATA);
        if (list != null) {
            int size = list.size();
            for (int i = 0; i < size; i++) {
                String path = list.get(i);
                linkedMap.put(String.valueOf(i), path);
            }
        }
        needRepeat = bundle.getBoolean(KEY_REPEAT);
        repeatCount = bundle.getInt(KEY_REPEAT_COUNT);
    }

    private void cancelTimer() {
        try {
            JZVideoPlayerManager.getCurrentJzvd().cancelProgressTimer();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
